package com.linghang.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/*工具类*/
public class MyBatis_Utils {

        private static SqlSessionFactory sqlSessionFactory;//提升作用域
         //获取sqlSessionFactory对象
        static
         {
                  String resource = "mybatis-config.xml";
                 InputStream inputStream;
                 try {

                         inputStream = Resources.getResourceAsStream(resource);
                         sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
                 } catch (IOException e) {
                         e.printStackTrace();
                 }
         }




        //既然有了SqlSessionFactory 我们就可以从中获取SqlSession 的实例了
        //SqlSession 完全包含了面向数据库执行sql命令 所需的所有方法
        public static SqlSession getSqlSession(){
                return sqlSessionFactory.openSession();
        }



}
